﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.NewPBAuto.Presenter;
using MINERP.MODEL.FoundationModel;
using DevExpress.XtraEditors.Controls;
using MINERP.MODEL.NewPinBan;
using MINERP.BLL.NewPB;

namespace MINERP.NewPBAuto.Forms
{
    public partial class FrmAutoPinban : Form
    {
        public FrmAutoPinban()
        {
            InitializeComponent();
        }
        private const int MaxOrderInfoNum = 20;
        AutoPB _AutoPinBanBll=new AutoPB();
        private NewPBBLL _NewPBBll=new NewPBBLL();
        private void FrmAutoPinban_Load(object sender, EventArgs e)
        {
            List<Tb_paperTemplate_Info> TemplateList = DataTransfer.TemplateList.Where(p => p.PaperFaceId == DataTransfer.curTemplate.PaperFaceId&&p.PaperTypeId==DataTransfer.curTemplate.PaperTypeId).ToList();

            CheckCmbSelectTemplete.Properties.DataSource = TemplateList;
            CheckCmbSelectTemplete.Properties.DisplayMember = "TemplateName";
            CheckCmbSelectTemplete.Properties.ValueMember = "TemplateId";
        }

        private void backgroundWorkerAutoPinBan_DoWork(object sender, DoWorkEventArgs e)
        {
            if (backgroundWorkerAutoPinBan.CancellationPending)
            {
                e.Cancel = true;
            }

            List<Tb_paperTemplate_Info> listTemp = new List<Tb_paperTemplate_Info>();

            foreach (CheckedListBoxItem item in CheckCmbSelectTemplete.Properties.Items)
            {
                if (item.CheckState == CheckState.Checked)
                {
                    listTemp.Add(DataTransfer.TemplateList.FirstOrDefault(p => p.TempLateId == (int)item.Value));

                }

            }
           
            Tb_paperTemplate_Info outBoardTemplete = new Tb_paperTemplate_Info();
            List<PhotoInfo_PinBan> sourceList = new List<PhotoInfo_PinBan>();
            while (true)
            {
                List<PhotoInfo_PinBan> listResult;
                decimal outWaste = 0;
                int currLength;
                DateTime stTime = DateTime.Now;

                //获取要拼版的照片在数据库中查找 前20个产品的照片获取规则：1已经拼过的产品2加急产品3开单时间
                backgroundWorkerAutoPinBan.ReportProgress(1, "正在获取要拼版的照片");
                int currOrderNum=sourceList.GroupBy(g => g.OrderProductId).Count();
                if (currOrderNum < MaxOrderInfoNum)
                {
                    sourceList = _AutoPinBanBll.GetWaitPBPhotoList(DataTransfer.curTemplate,DataTransfer.CurPrinter.PrinterID,MaxOrderInfoNum - currOrderNum);
                }
                listResult = _AutoPinBanBll.AutoPinSN(sourceList, DataTransfer.curTemplate, DataTransfer.CurPrinter, (decimal)0.03, out outWaste,out currLength, 30, false, 0);
                if ((listResult != null && CheckPhoto(listResult)) || outWaste < 0 || outWaste == 1)
                {
                    listResult.ForEach(p => p.IsInBoard = false);
                }
                else
                {
                    int Boardid = _NewPBBll.CreateBoard(listResult, outBoardTemplete, DataTransfer.CurPrinter,0,(float)outWaste, currLength);
                    if(Boardid!=0)//添加成功
                    {

                    }
                    else
                    {
                        //版保存不成功
                    }
                }
               
            }

        }
        private bool CheckPhoto(List<PhotoInfo_PinBan> inList)
        {
            int iCount = inList.Count;
            for (int i = 0; i < iCount; i++)
            {
                for (int j = i + 1; j < iCount; j++)
                {
                    //i j位置进行判断矩形是否重合
                    // return true;
                    PhotoInfo_PinBan P1 = inList[i];
                    PhotoInfo_PinBan P2 = inList[j];

                    //两个矩形框的边的一半
                    decimal halfP1W = (P1.IsWhirl == true) ? P1.PixFactHeigth / 2 : P1.PixFactWidth / 2;
                    decimal halfP2W = (P2.IsWhirl == true) ? P2.PixFactHeigth / 2 : P2.PixFactWidth / 2;
                    decimal halfP1H = (P1.IsWhirl == true) ? P1.PixFactWidth / 2 : P1.PixFactHeigth / 2;
                    decimal halfP2H = (P2.IsWhirl == true) ? P2.PixFactWidth / 2 : P2.PixFactHeigth / 2;

                    //两个矩形框的中心位置
                    decimal c1x = P1.Location.X + halfP1W;
                    decimal c1y = P1.Location.Y + halfP1H;
                    decimal c2x = P2.Location.X + halfP2W;
                    decimal c2y = P2.Location.Y + halfP2H;
                    //横向判断
                    if (Math.Abs(c1x - c2x) >= halfP1W + halfP2W || Math.Abs(c1y - c2y) >= halfP1H + halfP2H)
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }


                }
            }
            return false;
        }
        private void backgroundWorkerAutoPinBan_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }

        private void backgroundWorkerAutoPinBan_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (simpleButtonStartEnd.Text == "开始")
            {
                backgroundWorkerAutoPinBan.RunWorkerAsync();
                simpleButtonStartEnd.Text = "停止";
            }
            else {
                backgroundWorkerAutoPinBan.CancelAsync();
                simpleButtonStartEnd.Text = "开始";
            }
        }
    }
}
